import json
from urllib import request
import requests
from requests.auth import HTTPBasicAuth
import re

# 二、Urllib使用
# 1．发起基础请求
# url = 'http://httpbin.org/get?name=fcb'
# response = request.urlopen(url)

# 2．Request实例 中使用headers和method
# url = 'http://httpbin.org/get?name=fcb'
# res = request.Request(url, headers={
#     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
# }, method='GET')
# response = request.urlopen(res)


# 3．POST请求中使用content-type
# url = 'http://httpbin.org/post?name=fcb'
# data = {'age':'21', 'like':'eat'}
# json_data = json.dumps(data)
# res = request.Request(url, data=json_data.encode('utf-8'), headers={
# 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
# 'content-type':'application/json'
# }, method='POST')
# response = request.urlopen(res)

# if response.status == 200:
#     response_bytes = response.read()
#     response_text = response_bytes.decode('utf-8')
#     response_json = json.loads(response_text)
#     print(response_json)

# 三、Requests
# 1．Get请求的params与post请求的data与json案例
# url = 'http://httpbin.org/get'
# response = requests.get(url, params={
#     'name':'fcb',
#     'age': '21',
#     'like': 'eat'
# })
# print(response.text)

# url = 'http://httpbin.org/post'
# response = requests.post(url,
# data={
#     'name':'fcb',
#     'age': '21',
#     'like': 'eat'
# })
# print(response.json())

# response = requests.post(url,
# json={
#     'name':'fcb',
#     'age': '21',
#     'like': 'eat'
# }
# )
# print(response.json())


# 2．响应的content、text、json()案例
# url = 'http://httpbin.org/post'
# response = requests.post(url,
# json={
#     'name':'fcb',
#     'age': '21',
#     'like': 'eat'
# }
# )
# print(response.content)
# print(response.text)
# print(response.json())


# 3．allow_redirects、headers、cookies、auth参数使用案例
# url = 'http://127.0.0.1:8000/admin/'
# response = requests.get(url, allow_redirects=False)
# print(response.status_code)
# print(response.headers.get('location'))
# location = response.headers.get('location')
# location = f'http://127.0.0.1:8000{location}'
# response = requests.get(location)
# print(response.status_code)

# headers = {
    # 'cookie':'sessionid=js4no6j5vl1py9ytmgxb3giuwuoat70u'
    # }
# cookies = {'sessionid':'js4no6j5vl1py9ytmgxb3giuwuoat70u'}
# response = requests.get(url, headers=headers, cookies=cookies)
# response = requests.get(url,auth=HTTPBasicAuth('admin','password'))
# print(response.status_code)
# print(response.text)

import re
# 4．模拟登录
# url = 'http://127.0.0.1:8000/admin/login/?next=/admin/'
# response = requests.get(url)
# csrfmiddlewaretoken = re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">',response.text).group(1)
# cookies = response.cookies
# response = requests.post(url, data={
#     'csrfmiddlewaretoken': csrfmiddlewaretoken,
#     'username': 'admin',
#     'password': '123456',
# },
#     cookies=cookies
# )
# print(response.text)


# 5．结合后端网站验证什么是Referer
url = 'http://127.0.0.1:8000/admin/'
headers = {
    'cookie':'sessionid=2d8zm2pl3zzlr9tq6ny1s26qjru0w19l',
    'Referer':'http://127.0.0.1:8000/admin/login/?next=/admin/'
    }
response = requests.get(url, headers=headers)
response_text = response.text
print(response_text)
